<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

include('includes/Admin.Core.php');

CheckAccess('managecategory');


PrintHeader('分类目录');

$action = ForceIncomingString('action', 'displaydefault');

@set_time_limit(800);

// ############################## FUNCTION #############################

function GetCategorySelect($currentid, $selectedid =0, $showzerovalue = 1, $selectname = 'parentid'){
	global $DB;

	$sReturn = '<select name="' . $selectname . '">';

	if($showzerovalue){
		$sReturn .= '<option value="0">无</option>';
	}

	$getcategories = $DB->query("SELECT categoryid, parentid, title  FROM " . TABLE_PREFIX . "categories ORDER BY ordernum");
	$icategories = $DB->getrows($getcategories);

	$sReturn .= GetOptions($icategories, $currentid, $selectedid);
	$sReturn .= '</select>';

	return $sReturn;
}

function GetOptions($icategories, $currentid = 0, $selectedid = 0, $parentid = 0, $sublevelmarker = ''){
	if($parentid) $sublevelmarker .= '&minus;&minus;';

	foreach($icategories as $value){
		if($parentid == $value['parentid'] AND $value['categoryid'] != $currentid){
			$sReturn .= '<option '. Iif(!$parentid, 'style="color:orange;font-weight:bold;"') . ' value="' . $value['categoryid'] . '" ' . Iif($selectedid == $value['categoryid'], 'SELECTED', '') . '>' . $sublevelmarker . $value['title'] . '</option>';

			$sReturn .= GetOptions($icategories, $currentid, $selectedid, $value['categoryid'], $sublevelmarker);
		}
	}

	return $sReturn;
}


// ########################### UPDATE CATEGORIES ############################

if($action == 'updatecategories'){
	$categoryids   = ForceIncomingArray('categoryids');
	$ordernums   = ForceIncomingArray('ordernums');
	$activeds   = ForceIncomingArray('activeds');
	$titles   = ForceIncomingArray('titles');
	$sortings   = ForceIncomingArray('sortings');

	$categorycount = count($categoryids);


    for($i = 0; $i < $categorycount; $i++)
    {
		$DB->query("UPDATE " . TABLE_PREFIX . "categories SET ordernum = '". ForceInt($ordernums[$i])."',
		actived = '$activeds[$i]',
		title = '". ForceString($titles[$i])."',
		sorting = '$sortings[$i]'
		WHERE categoryid = '$categoryids[$i]'");
    }

    PrintRedirect('admin.icategories.php', 1);

}


// ############################## DELETE category #############################

if($action == 'deletecategory')
{
	$categoryid = ForceIncomingInt('categoryid');
	$parentid = ForceIncomingInt('parentid');

	if($subcategory = $DB->query_first("SELECT categoryid FROM " . TABLE_PREFIX . "categories WHERE parentid = '$categoryid'")){
		$errors = "此分类有下级目录, 无法删除! 请先删除其下级目录.";
		PrintErrors($errors, '删除目录错误');
		$action = 'displaydefault';
	}else{

		if(IsPost('confirmcancel'))
		{
			$action = 'displaydefault';

		}else if(IsPost('confirmdelete'))	{

			$DB->query("DELETE FROM " . TABLE_PREFIX . "categories WHERE categoryid = '$categoryid' ");

			if($parentid != 0){

				$DB->query("UPDATE " . TABLE_PREFIX . "images SET categoryid = '$parentid' WHERE categoryid = '$categoryid' ");

			}else{

				$getimages = $DB->query("SELECT imageid FROM " . TABLE_PREFIX . "images WHERE categoryid = '$categoryid' ");

				while($image = $DB->getrow($getimages)){
					DeleteImageById ($image['imageid']);
				}

			}

			PrintRedirect('admin.icategories.php', 1);

		}else{

			$category = $DB->query_first("SELECT parentid, title  FROM " . TABLE_PREFIX . "categories WHERE categoryid = '$categoryid'");

			PrintSection('删除分类目录');

			echo '<form method="post" action="admin.icategories.php">
			<input type="hidden" name="categoryid" value="' . $categoryid . '" />
			<table width="100%" border="0" cellpadding="5" cellspacing="0">
			<tr><td class="tdrow1" colspan="2">确认删除</td></tr>
			<tr>
			<td class="tdrow2" width="60%" valign="top"><b>确定删除图片分类目录: "<font class=ohredb>' . $category['title'] . '</font>" 吗?</b><br /><br />';

			if($othercategory = $DB->query_first("SELECT categoryid FROM " . TABLE_PREFIX . "categories WHERE  categoryid != '$categoryid' AND parentid = 0 ")){
				echo '此目录下的所有图片将自动转入: ' . GetCategorySelect($categoryid, $category['parentid'], 0);
			}else{
				echo '注意: <font class=ohredb>此目录下的所有图片将被删除!</font>';
			}

			echo '</td>
			<td class="tdrow3" valign="top">
			
			<input type="hidden" name="action" value="deletecategory" />
			<input type="submit" name="confirmdelete" value=" 确定删除 " />&nbsp;&nbsp;
			<input type="submit" name="confirmcancel" value=" 取消 " />
			
			</td>
			</tr>
			</table></form>';
			EndSection();

		}

	}
}



// ############################# INSERT OR EDIT category ###########################

if($action == 'insertcategory' OR $action == 'updatecategory')
{
	$categoryid = ForceIncomingInt('categoryid');
	$parentid = ForceIncomingInt('parentid');
	$ordernum = ForceIncomingInt('ordernum');
	$actived = ForceIncomingInt('actived');
	$title  = ForceIncomingString('title');
	$description  = ForceIncomingString('description');
	$keywords  = ForceIncomingString('keywords');
	$datasize = ForceIncomingInt('datasize');
	$sorting  = ForceIncomingString('sorting');
	$watermark = ForceIncomingInt('watermark');
	$watermarkpos = ForceIncomingInt('watermarkpos');

	if ($title == '')
	{
		$errors = "分类目录名称不能为空!";
	}

	if($errors){

		if($action == 'insertcategory'){
			PrintErrors($errors, '创建目录错误');
			$action = 'createcategory';
		}else if($action == 'updatecategory'){
			PrintErrors($errors, '编辑目录错误');
			$action = 'editcategory';
		}

	}else{
		if($action == 'insertcategory'){
			$DB->query("INSERT INTO " . TABLE_PREFIX . "categories VALUES (NULL, '$parentid', '$ordernum', '$actived', '$title', '$description', '$keywords', '$datasize', '$sorting', '$watermark', '$watermarkpos') ");

			$categoryid = $DB->insert_id();
			$DB->query("UPDATE " . TABLE_PREFIX . "categories SET ordernum     = '$categoryid' WHERE categoryid = '$categoryid'");

		}else if($action == 'updatecategory'){
			$DB->query("UPDATE " . TABLE_PREFIX . "categories SET 
			parentid= '$parentid',
			ordernum     = '$ordernum',
			actived     = '$actived',
			title     = '$title',
			description     = '$description',
			keywords     = '$keywords',
			datasize     = '$datasize',
			sorting     = '$sorting',
			watermark     = '$watermark',
			watermarkpos     = '$watermarkpos'
			WHERE categoryid   = '$categoryid'");
		}

		PrintRedirect('admin.icategories.php', 1);
	}

}

// ############################## DISPLAY DEFAULT ##############################

if($action == 'displaydefault')
{
	$getcategories = $DB->query("SELECT c.categoryid, c.parentid, c.ordernum, c.actived, c.title, c.sorting, COUNT(i.imageid) AS images FROM " . TABLE_PREFIX . "categories c LEFT JOIN " . TABLE_PREFIX . "images i ON (i.categoryid = c.categoryid) GROUP BY c.categoryid ORDER BY c.ordernum");
	$totals = $DB->get_num_rows();

	PrintSection('添加分类目录');
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" colspan="2">添加分类目录</td>
	</tr>
	<tr>
	<td class="tdrow2" width="60%">添加新的图片分类目录:</td>
	<td class="tdrow3">
	<form method="post" action="admin.icategories.php">
	<input type="hidden" name="action" value="createcategory" />
	<input type="submit" value=" 添加目录 " />
	</form>
	</td>
	</tr>
	</table>';
	EndSection();

	PrintSection('分类目录列表('.$totals.'个)');

	echo '<form method="post" action="admin.icategories.php">
	<input type="hidden" name="action" value="updatecategories" />
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" nowrap="nowrap">编号与名称</td>
	<td class="tdrow1" nowrap="nowrap">图片数</td>
	<td class="tdrow1" nowrap="nowrap">状态</td>
	<td class="tdrow1" nowrap="nowrap">图片排序</td>
	<td class="tdrow1" nowrap="nowrap">设置</td>
	<td class="tdrow1" nowrap="nowrap">删除</td>
	</tr>';

	if($totals < 1){
		echo '<tr><td class="tdrow3" colspan="9" align="center" height="48" valign="middle"><font class=ohredb>暂无任何图片分类目录!</font></td></tr>';
	}else{
		while($category = $DB->getrow($getcategories)){
			$icategories[$category['categoryid']] = $category;
			$parentids[$category['categoryid']] = $category['parentid'];
		}

		ShowCategories();
	}

	echo '<tr><td class="tdrow1" colspan="9" align="center"><input type="submit" value=" 保存更新 " /></td></tr></table></form>';

	EndSection();
}

function ShowCategories ($parentid = 0, $sublevelmarker = '') {
	global $icategories, $parentids;

	if($parentid) $sublevelmarker .= '<img src="' . TEMPLATEPATH . 'images/sub.gif" align="absmiddle">';

	$allcategories = $icategories;

	foreach($allcategories as $key => $value){
		if($parentid == $value['parentid']){
			echo '<tr>
			<td class="tdrow2">' . $sublevelmarker . '<input type="hidden" name="categoryids[]" value="' . $key . '" /><input type="text" name="ordernums[]" value="' . $value['ordernum'] . '"  size="3" />&nbsp;&nbsp;<input type="text" name="titles[]" value="' . $value['title'] . '" size="18" /></td>
			<td class="tdrow3">' . $value['images'] . '</td>
			<td class="tdrow3"><select name="activeds[]">
			<option value="1">发布</option>
			<option style="color:red;" value="0" ' . Iif(!$value['actived'], 'SELECTED', '') . '>隐藏</option>
			</select></td>
			<td class="tdrow3"><select name="sortings[]">
			<option value="odown"  '.Iif($value['sorting'] == 'odown', 'selected') .'>编号(降)</option>
			<option value="oup"  '.Iif($value['sorting'] == 'oup', 'selected') .'>编号(升)</option>
			<option value="tdown"  '.Iif($value['sorting'] == 'tdown', 'selected') .'>时间(降)</option>
			<option value="tup"  '.Iif($value['sorting'] == 'tup', 'selected') .'>时间(升)</option>
			</select></td>
			<td class="tdrow3"><a href="admin.icategories.php?action=editcategory&categoryid=' . $key . '"><img src="' . TEMPLATEPATH . 'images/configure.gif" /></a></td>
			<td class="tdrow2"><a href="admin.icategories.php?action=deletecategory&categoryid=' . $key . '"><img src="' . TEMPLATEPATH . 'images/trash.gif" /></a></td>
			</tr>';

			ShowCategories($key, $sublevelmarker);
		}
	}
}

// ########################## DISPLAY category FORM ###########################

if($action == 'createcategory' OR $action == 'editcategory')
{
	if(isset($errors))
	{
		$category = array('categoryid'   => $categoryid,
		'parentid'   => $parentid,
		'ordernum'     => $ordernum,
		'actived'     => $actived,
		'title'     => $title,
		'description'     => $_POST['description'],
		'keywords'     => $keywords,
		'datasize'     => $datasize,
		'sorting'     => $sorting,
		'watermark'     => $watermark,
		'watermarkpos'     => $watermarkpos);

	}else if($action == 'editcategory'){
		$categoryid = ForceIncomingInt('categoryid');
		$category = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "categories WHERE categoryid = '$categoryid'");
	}else{
		$category = array();
		$category['categoryid'] = 0;
		$category['parentid'] = 0;
		$category['datasize'] = 2048;
		$category['watermark'] = 1;
		$category['watermarkpos'] = 0;
		$category['actived'] = 1;
		$category['sorting'] = 'orderdown';
	}

	if($categoryid)
	{
		PrintSection('编辑分类目录: ' . $category['title']);
	}else{
		PrintSection('添加分类目录');
	}

	echo '<script src="'.BASEURL.'library/editor/nicEdit.js" type="text/javascript"></script>
	<script type="text/javascript">
	bkLib.onDomLoaded(function() {
		new nicEditor({fullPanel:true, iconsPath: \''.BASEURL.'library/editor/nicEditorIcons.gif\', maxHeight:120}).panelInstance("description");
	});
	</script>';

	echo '<form method="post" action="admin.icategories.php" name="categories">
	<input type="hidden" name="action" value="' . Iif($action == 'createcategory', 'insertcategory', 'updatecategory') . '" />
	<input type="hidden" name="categoryid" value="' . $category['categoryid'] . '" />

	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" colspan="2">目录名称</td>
	</tr>
	<tr>
	<td class="tdrow2">输入图片分类目录的名称(<font class=ohred>必填项</font>):</td>
	<td class="tdrow3"><input type="text" name="title" value="' . $category['title'] . '"  size="30" /></td>
	</tr>
	<tr>
	<td class="tdrow1" colspan="2">父目录</td>
	</tr>
	<tr>
	<td class="tdrow2">选择当前目录的上级目录:</td>
	<td class="tdrow3">' . GetCategorySelect($category['categoryid'], $category['parentid']) . '</td>
	</tr>	';

	if($categoryid)
	{
		echo '<tr>
		<td class="tdrow1" colspan="2">目录编号</td>
		</tr>
		<tr>
		<td class="tdrow2">分类目录的排序编号:</td>
		<td class="tdrow3"><input type="text" name="ordernum" value="' . $category['ordernum'] . '"  size="16" /></td>
		</tr>	';
	}

	echo '<tr>
	<td class="tdrow1" colspan="2">图片排序方式</td>
	</tr>
	<tr>
	<td class="tdrow2">本分类目录下显示图片时的排序方式:</td>
	<td class="tdrow3">
	<select name="sorting">
	<option value="odown"  '.Iif($category['sorting'] == 'odown', 'selected') .'>编号(降)</option>
	<option value="oup"  '.Iif($category['sorting'] == 'oup', 'selected') .'>编号(升)</option>
	<option value="tdown"  '.Iif($category['sorting'] == 'tdown', 'selected') .'>时间(降)</option>
	<option value="tup"  '.Iif($category['sorting'] == 'tup', 'selected') .'>时间(升)</option>
	</select>
	</td>
	</tr>
	<tr>
	<td class="tdrow1" colspan="2">文件大小限制</td>
	</tr>
	<tr>
	<td class="tdrow2">允许上传到本分类目录的图片文件大小限制(Kb):</td>
	<td class="tdrow3"><input type="text" name="datasize" value="' . $category['datasize'] . '"  size="16" />&nbsp;(注: 1M = 1024kb)</td>
	</tr>
	<tr>
	<td class="tdrow1" colspan="2">是否水印?</td>
	</tr>
	<tr>
	<td class="tdrow2">前台用户发表到本类别的图片是否在显示大图时自动添加水印?</td>
	<td class="tdrow3"><input type="checkbox" name="watermark" value="1" '.Iif($category['watermark'] == 1, 'CHECKED').'></td>
	</tr>
	<tr>
	<td class="tdrow1" colspan="2">水印位置</td>
	</tr>
	<tr>
	<td class="tdrow2">本类别图片显示水印的位置:</td>
	<td class="tdrow3">
	<select name="watermarkpos">
	<option value="0"  '.Iif($category['watermarkpos'] == '0', 'selected') .'>中间</option>
	<option value="1"  '.Iif($category['watermarkpos'] == '1', 'selected') .'>左上</option>
	<option value="2"  '.Iif($category['watermarkpos'] == '2', 'selected') .'>右上</option>
	<option value="3"  '.Iif($category['watermarkpos'] == '3', 'selected') .'>左下</option>
	<option value="4"  '.Iif($category['watermarkpos'] == '4', 'selected') .'>右下</option>
	</select>
	</td>
	</tr>
	<tr>
	<td class="tdrow1" colspan="2">是否发布?</td>
	</tr>
	<tr>
	<td class="tdrow2">是否发布本目录? 当不发布时, 本目录下的图片将隐藏.</td>
	<td class="tdrow3"><input type="checkbox" name="actived" value="1" '.Iif($category['actived'] == 1, 'CHECKED').'></td>
	</tr>
	<tr>
	<td class="tdrow1" colspan="2">Meta关键字</td>
	</tr>
	<tr>
	<td class="tdrow2">分类目录的Meta关键字(便于搜索引擎收录, 请用英文逗号隔开):</td>
	<td class="tdrow3"><input type="text" name="keywords" value="' . $category['keywords'] . '"  size="30" /></td>
	</tr>
	<tr>
	<td class="tdrow1" colspan="2">目录描述</td>
	</tr>
	<tr>
	<td class="tdrow2">分类目录的描述性文字:</td>
	<td class="tdrow3" style="background: #fff;"><textarea name="description" cols="62" style="height:110px" id="description">'.$category['description'].'</textarea></td>
	</tr>	';

	echo '<tr><td class="tdrow1" colspan="2" align="center"><input type="submit" name="" value="' . Iif($action == 'createcategory', ' 创建目录 ', ' 保存更新 ') . '" /></td></tr></table></form>';
  
	EndSection();

}

// ############################### PRINT FOOTER ################################

PrintFooter();

?>